<template>
	<view class="content">
		<view class="bg"  :style="{'background': 'url('+zhuceImg+')','background-size':size}">
			<view class="cont">
			</view>
		</view>
		<view class="con">
			<!-- <view class="uni-form-item uni-column">
				<input type="text" class="uni-input" name="" v-model="name" placeholder="请填写姓名" />
			</view> -->
			<view class="uni-form-item uni-column">
				<input type="tel" class="uni-input" name="" v-model="phone" placeholder="请输入手机号" />
			</view>
			<view class="uni-form-item uni-column column-with-btn">
				<input type="number" class="uni-input" name="" v-model="pass" placeholder="请输入密码" />
				<!-- <button :class="{active : !disableCodeBtn}"  @click="getCode">{{codeBtn.text}}</button> -->
			</view>
			<!-- <button type="primary" @click="sendVc">免费领取</button> -->
			<button type="primary" @tap="getUserProfile">立即注册</button>
		</view>
	</view>
</template>

<script>
	let App = getApp();
	export default {
		data() {
			return {
				seconds: 120,
				codeBtn: {
					text: '获取验证码',
					waitingCode: false,
					count: this.seconds
				},
				phone:'',
				vc:'',
				pid:0,
				name:'',
				timer:'',
				friend:'',
				zhuceImg:'https://vreye.oss-cn-beijing.aliyuncs.com/image/user/dlzc.jpg',
				size:'100% 100%',
				pass:''
			}
		},
		onUnload() {
			if(this.timer) {  
				clearTimeout(this.timer);  
				this.timer = null;  
			}  
		},
		
		onLoad(optsions) {
			const scene = decodeURIComponent(optsions.scene)
			this.pid = scene;
			
			this.getimg10();
			// #ifdef MP-WEIXIN
			this.getOpenid();
			//#endif
		},
		methods: {
			getUserProfile() {
			    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
			    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
			    wx.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res) => {
						console.log(res)
						if (res.errMsg === "getUserProfile:ok") {
							this.sendVc(res.userInfo.avatarUrl,res.userInfo.nickName)
							
						}
					}
			    })
			},
			getimg10(){
				
				uni.showLoading({
					title:"加载中"
				})
				
				this.$api.interfaceApi('getImg')({
					'sys':App.globalData.sys,
					'index':10
				}).then(res=>{
					uni.hideLoading()
					if(res.code == 200){
						this.zhuceImg =  res.data
					}
				
				})
			},
			
			//获取openid
			getOpenid(){
				let backData
				uni.login({
					provider: 'weixin',
					success :(res)  => {
						this.code = res.code
					}
				});
			},
			getUrlParam(name) {
				var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
				let url = window.location.href.split('#')[0]
				let search = url.split('?')[1]
				if (search) {
					var r = search.substr(0).match(reg)
					if (r !== null){
						return unescape(r[2])
					}	
					return null
				} else{
					return null
				}	
			},
			
			
			
			
			getCode: function () {
				
				let phone = this.phone
				var reg = /^1[3456789]\d{9}$/;
				if (phone == "") {
					uni.showToast({
						title: "手机号不能为空",
						icon: "none"
					})
				} else if (!reg.test(phone)) {
					uni.showToast({
						title: "请输入有效手机号",
						icon: "none"
					})
				} else {
					
					let result = this.$api.interfaceApi('sendSms')({
						'mobile': phone,
						'admin_id':App.globalData.sys
					})
					if(result.code == 0){
						uni.showToast({
							title:result.msg,
							icon:'none'
						})
					}
					
					this.codeBtn.waitingCode = true;
					this.codeBtn.count = this.seconds;
					this.codeBtn.text = this.codeBtn.count + 's';
					
					let countdown = setInterval( () => {
						this.codeBtn.count--;
						this.codeBtn.text = this.codeBtn.count + 's';
						if( this.codeBtn.count < 0 ){
							clearInterval(countdown);
							this.codeBtn.text = '重新发送';
							this.codeBtn.waitingCode = false;
						}
					},1000);
				}	
			},
					
			
			async sendVc(head_img,nickName){
				let pass = this.pass
				let phone = this.phone
				var reg = /^1[3456789]\d{9}$/;
				
				if (phone == "") {
					uni.showToast({
						title: "手机号不能为空",
						icon: "none"
					})
					return;
				} else if (!reg.test(phone)) {
					uni.showToast({
						title: "请输入有效手机号",
						icon: "none"
					})
					return;
				} 
				if (pass == "") {
					uni.showToast({
						title: "请填入密码",
						icon: "none"
					})
					return;
				}
				
				let result = await this.$api.interfaceApi('sendVc')({
					//'vc': vc,
					'mobile': phone,
					'code':this.code,
					"headImg":head_img,
					"nickname":nickName,
					//'name':name,
					"pid":this.pid,
					'admin_id':App.globalData.sys,
					'password':pass,
				})
				if (result.code == 200) {
					uni.setStorageSync('userinfo',result.data)
					uni.showToast({
						title: result.msg,
						icon:'none'
					})
					
					setTimeout(() => {
						uni.switchTab({
							url:'/pages/index/index'
						})
					}, 1000);
					
				}else{
					uni.showToast({
						title: result.msg,
						icon:'none'
					})
				}
			}
		},
		computed: {
			
		}
	}
</script>

<style lang="scss" scoped>
	$color-primary: #1FD188;
	body{
		background-color: #FFFFFF;
	}
	.content{
		.bg{
			width: 100%;
			height: 840rpx;
			background-image: url(https://vreye.oss-cn-beijing.aliyuncs.com/image/user/dlzc.jpg);
			background-size: 100% 100%;
			overflow: hidden;
			.cont{
				color: #FFFFFF;
				margin: 309rpx auto 0;
				font-size: 26rpx;
				text-align: center;
			}
		}
	}
	
	.con{
		width: 90%;
		margin: -160rpx auto;
		background-color: #FFFFFF;
		padding: 40rpx;
		box-sizing: border-box;
		border-radius: 30rpx;
	}
	
	.uni-form-item{
		margin-bottom: 40upx;
		padding: 0;
		border-bottom: 1px solid #e3e3e3;
		.uni-input{
			font-size: 30upx;
			padding: 7px 0;
		}
	}
	.column-with-btn{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		button{
			font-size: 24upx;
			margin: 0;
			text-align: center;
			&:after{
				border: none
			}
			&.active{
				background-color: $color-primary;
				color: $uni-text-color-inverse;
			}
		}
	}
	.img-captcha{
		width: 150upx;
		height: 60upx;
	}
	button[type="primary"]{
		background-color: $color-primary;
		border-radius: 0;
		font-size: 34upx;
		margin-top: 60upx;
	}
	.links{
		text-align: center;
		margin-top: 40upx;
		font-size: 26upx;
		color: #999;
		view{
			display: inline-block;
			vertical-align: top;
			margin: 0 10upx;
		}
		.link-highlight{
			color: $color-primary
		}
	}
</style>
